package com.chat.service;

import com.chat.Local;
import com.chat.common.Group;
import com.chat.common.Message;
import com.chat.common.MessageType;
import com.chat.view.FriendListData;
import javafx.scene.control.Alert;

import java.awt.*;
import java.util.Iterator;

import static com.chat.view.MainWindowController.friend;

/*
   退出群聊
 */
public class ExitGroup {
        public void exitgroup(String gorupid,String userid){
            set(gorupid,userid);
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("提示");
            alert.setHeaderText("恭喜");
            alert.setContentText("您已成功退出该群聊！");
            alert.showAndWait();
            int line=0;
            Iterator<FriendListData> iter=friend.iterator();
            while(iter.hasNext()){
                FriendListData friendListData=(FriendListData) iter.next();
                System.out.println(friendListData.getId());
                if(friendListData.getId().equals(gorupid)){
                    friend.remove(line);
                    break;
                }
                line++;
            }
        }
        public void set(String gorupid,String userid){
            Message msg=new Message();
            Group group=new Group();
            group.setId(gorupid);
            group.setAddid(userid);
            msg.setMesType(MessageType.MESSAGE_EXIT_GROUP);
            msg.setData(group);
            ClientConnectServerThread clientConnectServerThread=ManageClientConnectServerThread.getClientConnectServerThread(Local.user.getUsername());
            clientConnectServerThread.send(msg);
        }
}
